package com.leijie.nio;

import java.net.InetSocketAddress;
import java.net.ServerSocket;
import java.net.SocketAddress;
import java.nio.ByteBuffer;
import java.nio.channels.SelectionKey;
import java.nio.channels.Selector;
import java.nio.channels.ServerSocketChannel;
import java.nio.channels.SocketChannel;
import java.util.Iterator;
import java.util.Set;

/**
 * @author LeiJie
 * @time 2020-5-12 10:22
 * @desc Nio Selector
 */
public class NioTest12 {

    public static void main(String[] args) throws Exception{
        int[] ports = new int[5];

        ports[0] = 5000;
        ports[1] = 5001;
        ports[2] = 5002;
        ports[3] = 5003;
        ports[4] = 5004;

        Selector selector = Selector.open();

        for (int port : ports) {
            ServerSocketChannel serverSocketChannel = ServerSocketChannel.open();
            serverSocketChannel.configureBlocking(false);
            SocketAddress socketAddress = new InetSocketAddress(port);
            ServerSocket socket = serverSocketChannel.socket();
            socket.bind(socketAddress);
            serverSocketChannel.register(selector, SelectionKey.OP_ACCEPT);

            System.out.printf("端口[%d]开始监听...\n", port);
        }

        while (true){
            //返回关注事件的数量
            int numbers = selector.select();
            System.out.printf("Selector numbers:%d\n",numbers);
            Set<SelectionKey> selectionKeys = selector.selectedKeys();
            System.out.println("SelectionKeys:"+selectionKeys);
            Iterator<SelectionKey> it = selectionKeys.iterator();
            while (it.hasNext()){
                SelectionKey selectionKey = it.next();
                if (selectionKey.isAcceptable()){
                    ServerSocketChannel serverSocketChannel = (ServerSocketChannel) selectionKey.channel();
                    SocketChannel socketChannel = serverSocketChannel.accept();
                    socketChannel.configureBlocking(false);
                    socketChannel.register(selector,SelectionKey.OP_READ);
                    it.remove();
                    System.out.println("客户端链接："+socketChannel);
                }else if (selectionKey.isReadable()){
                    SocketChannel socketChannel = (SocketChannel) selectionKey.channel();
                    int byteRead = 0;
                    while (true){
                        ByteBuffer buffer = ByteBuffer.allocate(512);
                        buffer.clear();
                        int read = socketChannel.read(buffer);
                        if (read <=0) {
                            break;
                        }
                        buffer.flip();
                        socketChannel.write(buffer);
                        byteRead += read;
                        //删除事件
                        it.remove();
                    }
                    System.out.println("byteRead:"+byteRead+" "+socketChannel);
                }
            }
            selectionKeys.clear();
        }
    }

}
